package com.cseg674.dataquery;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.ArrayList; 

/**
 * Servlet implementation class for Servlet: ExtractorServlet
 *
 */
 public class ExtractorServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet 
 {
   static final long serialVersionUID = 1L;
   private static final int SUBJECT = 0;
   private static final int PREDICATE = 1;
   private static final int OBJECT = 2;
    
	public ExtractorServlet() 
	{
		super();
	}   	
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request, response);
	}  	
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html");
		HttpSession session = request.getSession(true);
		String prjName = request.getParameter("PrjName"); 
		String path = getServletContext().getRealPath("/ontology/source.owl");
		String subjOwl = request.getParameter("SubjOwl");
		String predOwl = request.getParameter("PredOwl");
		String objOwl = request.getParameter("ObjOwl");
		
		if(!path.contains(".owl"))
		{
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		else
		{
			Owl owl = new Owl(path); 
			
			ArrayList<String> subjList = new ArrayList<String>();
			ArrayList<String> predList = new ArrayList<String>();
			ArrayList<String> objList = new ArrayList<String>();
			ArrayList<String[]> subjobjList = new ArrayList<String[]>();
			
			// Get Subject and Object by Predicate
			if((predOwl != null) && (subjOwl == null) && (objOwl == null)){
				subjobjList = owl.extractSubjObjByPred(predOwl);
				String subjobjXml = getXml(subjobjList);
				response.setContentType("text/xml");
				response.getWriter().write(subjobjXml);
			}
			else if((objOwl != null) && (predOwl == null) && (subjOwl == null)){
				predList = owl.extractPredByObj(objOwl);
				writeXml(response, predList, PREDICATE);
			}
			else if((objOwl != null) && (predOwl != null) && (subjOwl == null)){
				subjList = owl.extractSubject(predOwl);
				writeXml(response, subjList, SUBJECT); 
			}
			else if((subjOwl != null) && (predOwl == null) && (objOwl == null)){
				predList = owl.extractPredBySubj(subjOwl);
				writeXml(response, predList, PREDICATE);
			}
			else if((subjOwl != null) && (predOwl != null) && (objOwl == null)){
				objList = owl.extractObject(predOwl);
				writeXml(response, objList, OBJECT); 
			}
			else{
				subjList = owl.extractAllSubject();
				predList = owl.extractAllPredicate();
				objList = owl.extractAllObject();
				session.setAttribute("OWLSubjList", subjList);
				session.setAttribute("OWLPredList", predList);
				session.setAttribute("OWLObjList", objList);
				request.setAttribute("PrjName", prjName);
				RequestDispatcher rd = request.getRequestDispatcher("sparql-search.jsp");
				rd.forward(request, response);
			}
		}
	}
	
	private void writeXml(HttpServletResponse response, ArrayList<String> partList, int part){
		try{
			String xml = getXml(partList, part);
			response.setContentType("text/xml");
			response.getWriter().write(xml);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	private String getXml(ArrayList<String> partList, int part){
		String tagName = "";
		switch(part){
			case SUBJECT:
			tagName = "subject";
			break;
			case PREDICATE:
				tagName = "predicate";
				break;
			case OBJECT:
				tagName = "object";
				break;
		}
		
		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version=\"1.0\"?>\n");
		xml.append("<triple>\n");
		for(int i=0; i < partList.size(); i++){
			xml.append("<"+tagName+">");
			xml.append(partList.get(i));
			xml.append("</"+tagName+">\n");
		}
		xml.append("</triple>\n");
		return xml.toString();
	}
	
	private String getXml(ArrayList<String[]> partList){
		String[] sa = partList.get(0);
		String[] oa = partList.get(1);
		
		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version=\"1.0\"?>\n");
		xml.append("<triple>\n");
		for(int i=0; i < sa.length; i++){
			xml.append("<subject>");
			xml.append(sa[i]);
			xml.append("</subject>\n");
		}
		for(int i=0; i < oa.length; i++){
			xml.append("<object>");
			xml.append(oa[i]);
			xml.append("</object>\n");
		}
		xml.append("</triple>\n");
		return xml.toString();
	}
 } 